<template>
  <view class="search-box">
    <uni-search-bar  class="search-input" placeholder="请输入搜索内容" 
    cancelButton="none" @input="input" cancel-text="删除">
    </uni-search-bar>
  </view>
  <!-- 搜索建议 -->
  <view class="sugg-list" v-if="searchResults.length !== 0">
    <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="goToDetail(item.goods_id)">
      <view class="goods-name">{{item.goods_name}}</view>
      <uni-icons type="forward" ></uni-icons>
    </view>
  </view>
  <!-- 搜索历史 -->
  <view class="history-box" v-else>
    <view class="history-title">
      <text>搜索历史</text>
      <uni-icons type="trash" @click="clearHistory()" size="20"></uni-icons>
    </view>
    <view class="history-list">
      <uni-tag @click="goToGoodsList(item)" class="history-list-item" :inverted="true" v-for="(item,i) in historys" :key="i" :text='item'/>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        searchResults: [],
        historyList:[]
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
      // this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]') 
    },
    methods:{
      input(e){
        clearInterval(this.timer)
        this.timer = setTimeout(()=>{
          this.kw = e
          this.getSearchList()
        },500)
      },
      async getSearchList(){
        if(this.kw === ''){
          this.searchResults = []
          return
        }
        const {data:res} = await uni.$http.get('/goods/qsearch',{query:this.kw})
        this.saveSearchHistory(this.kw)
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
      },
      goToDetail(goods_id){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods_id 
        })
      },
      saveSearchHistory(kw){
        const set = new Set(this.historyList)
        set.delete(kw)
        set.add(kw)
        this.historyList = Array.from(set)
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      clearHistory(){
        this.historyList = []
        uni.setStorageSync('kw',[])
      },
      goToGoodsList(kw){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    },
    computed:{
      historys(){
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
  color: red;
  background-color: #c00000;
  .search-input{
    border-radius: 30rpx;
  }
}

.sugg-list {
  padding: 0 5px;

  .sugg-item {
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      // 文字不允许换行（单行文本）
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用 ... 代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}

.history-box{
  .history-title{
    display: flex;
    justify-content: space-between;
    padding: 20rpx 10rpx;
  }
  .history-list{
    border-top: 1rpx solid black;
    padding: 20rpx 20rpx 0rpx 20rpx;
    .history-list-item{
      margin: 10rpx;
    }
  }
}

</style>
